#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

#include "noise_suppression.h"

#define FRAMESIZE 160
#define SAMPLERATE 16000

int main(int argc, char *argv[])
{
    FILE *fidr = fopen(argv[1], "rb");
	fseek(fidr, 0, SEEK_END); int d2 = ftell(fidr);
    fseek(fidr, 0, SEEK_SET); int d1 = ftell(fidr);	
    int frames = (d2-d1)/2/FRAMESIZE; // 10ms，16bit    
    FILE *fidw = fopen(argv[2], "wb");

    int16_t *x = (int16_t *)calloc(FRAMESIZE, sizeof(int16_t));
    int16_t *y = (int16_t *)calloc(FRAMESIZE, sizeof(int16_t));
    int16_t *ibuffer[1] = {x};
    int16_t *obuffer[1] = {y};

    NsHandle *handle = WebRtcNs_Create();
    WebRtcNs_Init(handle, (uint32_t)SAMPLERATE);
    WebRtcNs_set_policy(handle, 2);

    for (int n=0; n<frames; n++)
    {
        fread(x, sizeof(short), FRAMESIZE, fidr);
        WebRtcNs_Analyze(handle, ibuffer[0]);
        WebRtcNs_Process(handle, (const int16_t *const *)ibuffer, 1, obuffer);
        fwrite(y, sizeof(short), FRAMESIZE, fidw);
    }

    WebRtcNs_Free(handle);
    free(x); free(y);
    fclose(fidr); fclose(fidw);
    return 0;
}